﻿using DataDefine;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using UsingEventAggregator.Core;
using System.Windows.Media;
namespace TLDNs.ViewModels
{

    /// <summary>
    /// 设备信息 FF7B1FA2 :紫色  FF326CF3:选择色
    /// </summary>
    public struct DeviceInfoIndex
    {
        public bool Export { get; set; }
        public int Index { get; set; }
        public Brush FocusBackground { get; set; }
        public static Brush FocusedBg = new SolidColorBrush(Colors.Aqua);
        public static SolidColorBrush UnFocusedBg = new SolidColorBrush(Colors.Gainsboro);
        public DeviceInfo Info { get; set; }
        public DeviceInfoIndex(int i, DeviceInfo f)
        {
            Index = i;
            Info = f;
            Export = false;
            FocusBackground = UnFocusedBg;
        }
    }

    public class DeviceIndex : BindableBase
    {
        public int Index { get; set; }
        private Device _OneDevice;
        public Device OneDevice
        {
            get { return _OneDevice; }
            set { SetProperty(ref _OneDevice, value); }
        }
    }


    public class SystemInfoViewModel : CanDevRootModelBase
    {
        public SystemInfoViewModel(IContainerExtension Container, IRegionManager rm, IEventAggregator ea) : base(Container, rm, ea)
        {
            SFM = SFMDocument.Create();
            int C = SFM.Devices.Length;
            for (int i = 0; i < C; i++)
            {
                // 只有Add有用
                DataList.Add(new DeviceInfoIndex(i, SFM.Devices[i].DeviceInfo));
            }

        }

        protected override void UILoadedCommand_Sub(ResourceDictionary obj)
        {
            base.UILoadedCommand_Sub(obj);
            
        }

        private SFMDocument _SFM;
        public SFMDocument SFM
        {
            get { return _SFM; }
            set
            {
                SetProperty(ref _SFM, value);
                System_SFMVersion = $"{value.System}-{value.SFMVersion}";
                if (SFM.Devices.Length > 0)
                {
                    CurDevice = SFM.Devices[0];
                }
            }
        }

        private string _System_SFMVersion;
        public string System_SFMVersion
        {
            get { return _System_SFMVersion; }
            set
            {
                SetProperty(ref _System_SFMVersion, value);
            }
        }


        private ObservableCollection<DeviceInfoIndex> _DataList = new ObservableCollection<DeviceInfoIndex>();
        public ObservableCollection<DeviceInfoIndex> DataList
        {
            get { return _DataList; }
            set { SetProperty(ref _DataList, value); }
        }

        private Device _CurDevice;
        public Device CurDevice
        {
            get { return _CurDevice; }
            set
            {
                SetProperty(ref _CurDevice, value);
            }
        }

        private int? _SelectIndex = null;
        public DelegateCommand<int?> DeviceSelectChangeCommand => new DelegateCommand<int?>(DeviceSelectChangeCommand_Sub);
        private void DeviceSelectChangeCommand_Sub(int? dev)
        {
            _SelectIndex = dev;
            if (dev != null && dev.Value < SFM.Devices.Length)
            {
                CurDevice = SFM.Devices[dev.Value];

                App.UIDispatcherDoAction(()=> {
                    for (int i = 0; i < DataList.Count; i++)
                    {
                        var tmp = DataList[i];
                        tmp.FocusBackground = DeviceInfoIndex.UnFocusedBg;
                        DataList[i] = tmp;
                    }
                    var tmp2 = DataList[dev.Value];
                    tmp2.FocusBackground = DeviceInfoIndex.FocusedBg;
                    DataList[dev.Value] = tmp2;
                });
                
            }

        }

        protected override void SFMFileIsReLoaded()
        {
            SFM = SFMDocument.Create();

            DataList.Clear();
            int C = SFM.Devices.Length;
            string[] SFM_DeviceNames = new string[C];
            for (int i = 0; i < C; i++)
            {
                // 只有Add有用
                DataList.Add(new DeviceInfoIndex(i, SFM.Devices[i].DeviceInfo));
                SFM_DeviceNames[i] = SFM.Devices[i].DeviceInfo.DeviceName;
            }
            ErrorHistoryViewModel.RegeditSFMDevNames(SFM_DeviceNames);
        }

        public DelegateCommand AddDeviceCommand => new DelegateCommand(AddDeviceCommand_Sub);
        private void AddDeviceCommand_Sub()
        {
            var dev = new Device();
            DeviceInfoIndex devif = new DeviceInfoIndex(DataList.Count, dev.DeviceInfo);
            DataList.Add(devif);
            var devs = SFM.Devices.ToList();
            devs.Add(dev);
            SFM.Devices = devs.ToArray();
            SFMDocument.SetSFMDocument(SFM);

        }
        public DelegateCommand DeleteDeviceCommand => new DelegateCommand(DeleteDeviceCommand_Sub);
        private void DeleteDeviceCommand_Sub()
        {
            for (int i = 0; i < DataList.Count; i++)
            {
                if (DataList[i].Info == CurDevice.DeviceInfo)
                {
                    DataList.RemoveAt(i);
                    var devs = SFM.Devices.ToList();
                    devs.RemoveAt(i);
                    SFM.Devices = devs.ToArray();
                    SFMDocument.SetSFMDocument(SFM);
                    SFMFileIsReLoaded();
                }
            }
        }

        public DelegateCommand UpdateDeviceCommand => new DelegateCommand(UpdateDeviceCommand_Sub);
        private void UpdateDeviceCommand_Sub()
        {
            _ea.GetEvent<OpenSFMIsChangeEvent>().Publish();
            _ea.GetEvent<SaveSFMOkEvent>().Publish();//解决打开文件时就可以保存问题
        }

    }


}
